#!/usr/bin/python2.4
#
#Copyright 2009 Google Inc.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
#


"""Tests for the module metadata_handler."""

import logging
import unittest
from google.appengine.ext import db
import db_utils
from metadata_handler import UploadMetadataHandler
from metadata_in_memory import MetadataInMemory
import text_data_reader
from tree_node import PageTree


class Request(object):
  """Dummy Request class."""
  uri = ''


class Response(object):
  """Dummy Response class."""

  class Out(object):
    """Dummy Out class for the Response object."""
    text = ''

    def write(self, st):
      """Dummy write response method.

      Args:
        st: String representing html response.
      """
      self.text = st
  out = Out()


class MetadataHandlerTest(unittest.TestCase):
  """Tests the class MetadataHandler."""

  def setUp(self):
    """Set up method."""
    logging.info('TEST: ' + self.id())
    self.db_handler = db_utils.DbHandler()
    self.metadata_in_memory = MetadataInMemory()
    self.metadata_in_memory.Clear()
    self.page_tree = PageTree()

  def testUploadMetadaHandler(self):
    """Tests the GET method of the class UploadMetadataHandler."""
    version_id = self.db_handler.CreateNewVersion()
    q = db.GqlQuery('SELECT * FROM Metadata WHERE version_id=:1', version_id)
    self.assertEqual(q.count(), 0)
    f = open('test/data/metadata_valid.txt', 'r')
    text_data_reader.ReadMetadataFile(f, version_id)
    f.close()
    self.metadata_in_memory.UpdateMetadata({}, {}, {})
    self.upload_metadata_handler = UploadMetadataHandler()
    self.upload_metadata_handler.request = Request()
    self.upload_metadata_handler.request.uri = 'http://localhost:8080'
    self.upload_metadata_handler.response = Response()
    self.upload_metadata_handler.get()
    while True:
      out_text = self.upload_metadata_handler.response.out.text
      if out_text.find('/uploadmetadata') != -1:
        self.upload_metadata_handler = UploadMetadataHandler()
        self.upload_metadata_handler.request = Request()
        self.upload_metadata_handler.request.uri = 'http://localhost:8080'
        self.upload_metadata_handler.response = Response()
        self.upload_metadata_handler.get()
      else:
        q = db.GqlQuery('SELECT * FROM Metadata WHERE version_id=:1',
                        version_id)
        self.assertEqual(q.count(), 6)
        for metadata in q:
          if metadata.row_id > 0:
            self.assertEqual(metadata.version_id, version_id)
        break
    self.db_handler.RemoveVersion(version_id)
    self.page_tree.Clear()
    self.metadata_in_memory.Clear()


def main(unused_argv):
  unittest.main()
